<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Dog() { }

        // 写了一个新的函数 调用这个函数，每次得到一个狗对象


        // var dog = null;

        // var shareDog = function () {
        //     if (dog == null) {
        //         dog = new Dog()
        //     }
        //     return dog;
        // }


        // 立即执行函数，定义完立刻就调用
        (function () { })();

        var shareDog = (function () {
            var dog = null;//局部--闭包--像是全局变量一杨
            return function () {
                if (dog == null) {
                    dog = new Dog()
                }
                return dog;
            }
        })();

        console.log(shareDog);

        var d1 = shareDog();
        var d2 = shareDog()

        console.log(d1 === d2);


    </script>

</body>

</html>